﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.DomainModel;
using Enterprise.Framework.DomainModel.Repositories;
using Resto.CostControl.Core.Domain.Resource;
using Resto.CostControl.Core.Facade.MaterialModule.DataTransferObject;
using Resto.CostControl.Core.Cache;
using Resto.CostControl.Core.Facade.SupplierModule.DataTransferObject;
using Resto.CostControl.Core.Domain.Query.MaterialModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Facade.SiteModule.DataTransferObject;
using Resto.CostControl.Common.Utility;

namespace Resto.CostControl.Core.Domain.Model
{
    public partial class MaterialStorageHistory : IEntity
    {
        #region 构造函数

        internal MaterialStorageHistory() {
            this.ID = IDHelper.CreateNewID();
        }

        #endregion

        #region 私有属性

        private IEntityRepository<MaterialStorageHistory> _Repository;
        private IEntityRepository<MaterialStorageHistory> Repository
        {
            get
            {
                if (_Repository == null)
                    _Repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<MaterialStorageHistory>();

                return _Repository;
            }
        }

        #endregion

        #region 业务方法

        /// <summary>
        /// 根据采购单生成存储历史
        /// </summary>
        /// <param name="orderItem"></param>
        internal static void CreateStorageHistory(MaterialPurchaseItem orderItem)
        {
            Material material = orderItem.Material;

            SupplierDTO supplierDTO = SupplierDTOCache.Get(orderItem.MaterialPurchase.SupplierID);

            if (material != null && supplierDTO != null)
            {
                MaterialStorageHistory history = new MaterialStorageHistory()
                {
                    Count = orderItem.StorageCount.Value,
                    Content = string.Format(MaterialResource.MaterialStorageHistory_Purchase_Content,
                                            supplierDTO.Name,orderItem.StorageCount.Value,
                                            material.Unit, orderItem.MaterialPurchase.Code),
                    Title = MaterialResource.MaterialStorageHistory_Purchase_Title,
                    MaterialID = orderItem.MaterialID,
                    Time = orderItem.MaterialPurchase.OrderDate,
                };

                history.Add();

                material.ActualStorage += history.Count;
            }
        }

        /// <summary>
        /// 根据加工单生成库存历史
        /// </summary>
        /// <param name="orderItem"></param>
        internal static void CreateStorageHistory(MaterialProcessItem orderItem)
        {

            Material rawMaterial = orderItem.RawMaterial;
            Material semiMaterial = orderItem.SemiMaterial;

            if (rawMaterial != null && semiMaterial != null)
            {
                string content = string.Format(MaterialResource.MaterialStorageHistory_Process_Content,
                                    orderItem.ActualPickCount, rawMaterial.Unit, rawMaterial.Name,
                                    orderItem.ActualStorageCount, semiMaterial.Unit, semiMaterial.Name,
                                    orderItem.MaterialProcess.Code);
               
                //原材料的记录
                MaterialStorageHistory rawHistory = new MaterialStorageHistory()
                {
                    Count = orderItem.ActualPickCount.Value * -1,
                    Content = content,
                    Title = MaterialResource.MaterialStorageHistory_Process_Title,
                    MaterialID = orderItem.RawMaterialID,
                    Time = orderItem.MaterialProcess.ProcessDate,
                };

                rawHistory.Add();
                rawMaterial.ActualStorage += rawHistory.Count;

                //半成品的记录
                MaterialStorageHistory semiHistory = new MaterialStorageHistory()
                {
                    Count = orderItem.ActualStorageCount.Value,
                    Content = content,
                    Title = MaterialResource.MaterialStorageHistory_Process_Title,
                    MaterialID = orderItem.RawMaterialID,
                    Time = orderItem.MaterialProcess.ProcessDate,
                };

                semiHistory.Add();
                semiMaterial.ActualStorage += semiHistory.Count;
            }
        }

        /// <summary>
        /// 根据发货单生成存储历史
        /// </summary>
        /// <param name="orderItem"></param>
        internal static void CreateStorageHistory(DishDeliveryItem orderItem)
        {
            Material material = orderItem.Material;

            SiteDTO siteDTO = SiteDTOCache.Get(orderItem.DishDelivery.SiteID);

            if (material != null && siteDTO != null)
            { 
                MaterialStorageHistory history = new MaterialStorageHistory()
                {
                    Count = orderItem.DeliveryCount.Value * -1,
                    Content = string.Format(MaterialResource.MaterialStorageHistory_Delivery_Content,
                                            orderItem.DeliveryCount,material.Unit,
                                            material.Name, siteDTO.Name, orderItem.DishDelivery.Code),
                    Title = MaterialResource.MaterialStorageHistory_Purchase_Title,
                    MaterialID = orderItem.MaterialID,
                    Time = orderItem.DishDelivery.Date,
                };

                history.Add();

                material.ActualStorage += history.Count;
            }
        }

        protected void Add()
        {
            this.Repository.Add(this);
        }

        //internal void Update()
        //{
        //    this.Repository.Update(this);
        //}

        //internal void Delete()
        //{
        //    this.Repository.Delete(this);
        //}


        #endregion

    }
}
